package cn.llming.org.controller;


import cn.llming.auth.util.BasePermission;
import cn.llming.basic.query.BaseQuery;
import cn.llming.basic.result.BaseResult;
import cn.llming.org.domain.Employee;
import cn.llming.org.query.DepartmentQuery;
import cn.llming.org.service.IEmployeeService;
import cn.llming.vo.result.AjaxResult;
import cn.llming.vo.result.ListPager;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@BasePermission(name = "员工管理",desc = "员工管理权限")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    //增加/修改
//    RequestBody,代表从请求体获取数据
    @PostMapping
    @BasePermission(name = "员工增加/修改",desc = "员工增加/修改权限")
    public AjaxResult updateAndInsert(@RequestBody Employee employee){
        try {
            if(Objects.nonNull(employee.getId())){ //有id，就执行修改操作
                employeeService.update(employee);
            }else {//无id，就执行新增操作
                employeeService.insert(employee);
            }
            return AjaxResult.myRes();//给前端一个正常反馈
        }catch (Exception e){
            //给前端反馈错误信息
            return AjaxResult.myRes().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }

    }

    //删除

    @DeleteMapping("/{id}")
    @BasePermission(name = "员工删除",desc = "员工删除权限")
    public AjaxResult deleteEmployee(@PathVariable("id") Long id){
        try {
            //根据id删除员工
            employeeService.delete(id);
            return AjaxResult.myRes();//给前端一个正常反馈
        }catch (Exception e){
            //给前端反馈错误信息
            return AjaxResult.myRes().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }


    //单个查询
    @GetMapping("/{id}")
    @BasePermission(name = "员工单个查询",desc = "员工单个查询权限")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            //根据id查询员工
            Employee employee = employeeService.getById(id);
            return AjaxResult.myRes().setObject(employee);//给前端一个正常反馈并且将数据返回给前端
        }catch (Exception e){
            //给前端反馈错误信息
            return AjaxResult.myRes().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
    //批量删除接口

    @PatchMapping
    @BasePermission(name = "员工批量删除接口",desc = "员工批量删除接口")
    public AjaxResult patchDeleteEmp(@RequestBody List<Long> ids){
        try {
            //根据id删除
                employeeService.batchDelete(ids);
            return AjaxResult.myRes();//给前端一个正常反馈并且将数据返回给前端
        }catch (Exception e){
            //给前端反馈错误信息
            return AjaxResult.myRes().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }
    }

    //查询所有
    @GetMapping
    @BasePermission(name = "员工查询所有",desc = "员工查询所有权限")
    public AjaxResult getAll(){
        try {
            //查询所有员工
            List<Employee> employees = employeeService.getAll();

            return AjaxResult.myRes().setObject(employees);//给前端一个正常反馈并且将数据返回给前端

        }catch (Exception e){
            //给前端反馈错误信息
            return AjaxResult.myRes().setSuccess(false).setMessage("删除失败"+e.getMessage());

        }

    }

    //员工分页
    @PutMapping
    @ApiOperation(value = "分页")
    @BasePermission(name = "员工分页查询",desc = "员工分页查询权限")
    public AjaxResult getListPageEmp(@RequestBody BaseQuery query){
        try{
            BaseResult query1=employeeService.getPageList(query);
            return AjaxResult.myRes().setObject(query1);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询分页失败"+e.getMessage()).setSuccess(false);
        }
    }

}
